package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.GradeAuthJsfzMapper;
import com.zhijian.medical.entity.po.GradeAuthJsfz;
import com.zhijian.medical.entity.vo.request.GradeAuthJsfzSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.GradeAuthJsfzSearchVo;
import com.zhijian.medical.entity.vo.response.GradeAuthJsfzVo;
import com.zhijian.medical.service.GradeAuthJsfzService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 分级授权书-技术分子业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 10:40:23
 */
@Slf4j
@Service
public class GradeAuthJsfzServiceImpl implements GradeAuthJsfzService {

	@Autowired
    private GradeAuthJsfzMapper mapper;

    @Override
    public GradeAuthJsfz selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public GradeAuthJsfzVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(GradeAuthJsfz gradeAuthJsfz) {
        Date now = new Date();
        gradeAuthJsfz.setCreateTime(now);
        gradeAuthJsfz.setUpdateTime(now);
        return mapper.insert(gradeAuthJsfz) > 0 ? gradeAuthJsfz.getId() : 0L;
    }

    @Override
    public Long insertSelective(GradeAuthJsfz gradeAuthJsfz) {
        Date now = new Date();
        gradeAuthJsfz.setCreateTime(now);
        gradeAuthJsfz.setUpdateTime(now);
        return mapper.insertSelective(gradeAuthJsfz) > 0 ? gradeAuthJsfz.getId() : 0L;
    }

    @Override
    public boolean update(GradeAuthJsfz gradeAuthJsfz) {
        Date now = new Date();
        gradeAuthJsfz.setUpdateTime(now);
        return mapper.updateByPrimaryKey(gradeAuthJsfz) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(GradeAuthJsfz record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(GradeAuthJsfzSaveOrUpdateVo saveOrUpdateVo) {
        GradeAuthJsfz gradeAuthJsfz = new GradeAuthJsfz();
        BeanUtils.copyProperties(saveOrUpdateVo, gradeAuthJsfz);
        return this.insert(gradeAuthJsfz) != 0;
    }

    @Override
    public boolean modify(GradeAuthJsfzSaveOrUpdateVo saveOrUpdateVo) {
        GradeAuthJsfz old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        GradeAuthJsfz gradeAuthJsfz = new GradeAuthJsfz();
        BeanUtils.copyProperties(saveOrUpdateVo, gradeAuthJsfz);
        return this.update(gradeAuthJsfz);
    }

    @Override
    public GradeAuthJsfzVo selectOne(GradeAuthJsfzSearchVo gradeAuthJsfzSearchVo) {
        return mapper.selectOne(gradeAuthJsfzSearchVo);
     }

    @Override
    public Integer count(GradeAuthJsfzSearchVo gradeAuthJsfzSearchVo) {
        return mapper.count(gradeAuthJsfzSearchVo);
    }

    @Override
    public List<GradeAuthJsfzVo> selectList(GradeAuthJsfzSearchVo gradeAuthJsfzSearchVo) {
        return mapper.selectList(gradeAuthJsfzSearchVo);
    }

    @Override
    public Page<GradeAuthJsfzVo> selectPage(GradeAuthJsfzSearchVo gradeAuthJsfzSearchVo) {
        AssertUtil.check(Objects.isNull(gradeAuthJsfzSearchVo.getPageNum()) || Objects.isNull(gradeAuthJsfzSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(gradeAuthJsfzSearchVo);
        //分页
        Page<GradeAuthJsfzVo> page = new Page<>(true, count, gradeAuthJsfzSearchVo.getPageNum(), gradeAuthJsfzSearchVo.getPageSize());
        gradeAuthJsfzSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<GradeAuthJsfzVo> responseVos = this.selectList(gradeAuthJsfzSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<GradeAuthJsfz> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(GradeAuthJsfzSearchVo gradeAuthJsfzSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(gradeAuthJsfzSearchVo);
    }
    
}